#
#      zak - Transcode and concatinate multiple MP3 files
#
#      Copyright 2008 Joshua M. Hoffman - joshua@joshua.net
#
#      This program is free software; you can redistribute it and/or modify  
#      it under the terms of the GNU General Public License as published by  
#      the Free Software Foundation; either version 2 of the License, or  
#      (at your option) any later version.  
#  
#      This program is distributed in the hope that it will be useful,  
#      but WITHOUT ANY WARRANTY; without even the implied warranty of  
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
#      GNU General Public License for more details.  
#  
#      You should have received a copy of the GNU General Public License  
#      along with this program; if not, write to the Free Software  
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import gobject
import pygst
pygst.require('0.10')
import gst
import re
from gst.extend.discoverer import Discoverer

# Utility classes

class Prefs:
    
    # metadata
    metadata = {'title':'', 'author':'', 'comment':'Created by Zak', 'album':'', 'track':'', 'year':''}
    
    # preferences
    preferences = {'output_format':'m4a/m4b', 'output_bitrate':'96k', 'output_channels':'2',
                            'output_file':'output.m4b', 'madplay':'/usr/bin/madplay',
                            'ffmpeg':'/usr/bin/ffmpeg', 'terminal':False, 'output_samplerate':'22050'}
    
    # input files
    infiles = []
    
    # methods for class data manipulation
    def get_pref(self, pref):
        return Prefs.preferences[pref]
    
    def set_pref(self, pref, data):
        Prefs.preferences[pref] = data
        
    def get_meta(self, meta):
        return Prefs.metadata[meta]
    
    def set_meta(self, meta, data):
        Prefs.metadata[meta] = data
    
    def add_input_file(self, file):
        Prefs.infiles.append(file)
        
    def get_input_files(self):
        return Prefs.infiles
    
    def clear_input_files(self):
        Prefs.infiles = []

class Constants:
    
    myName = "Zak"
    myVersion = "0.1"
    
    
class AudioFile:
    
    def __init__(self, file):
        #gobject.threads_init()
        self.path = file
        self.duration = None
        self.rate = None
        self.channels = None
        self.tags = None
        self.valid = False
        self.working = True
        self.mimetype = ''
        # run
        self.mainloop = gobject.MainLoop()
        gobject.idle_add(self._inspect)
        self.mainloop.run()
        
    def _inspect(self):
        discoverer = Discoverer(self.path)
        discoverer.connect('discovered', self._on_discovered, file)
        discoverer.discover()
        return False # no repeating idle calls
    
    def _on_discovered(self, discoverer, ismedia, file):
        if discoverer.is_audio:
            self.duration = discoverer.audiolength
            self.rate = discoverer.audiorate
            self.channels = discoverer.audiochannels
            self.tags = discoverer.tags
            self.valid = True
            self.working = False
            self.mimetype = discoverer.mimetype
            self.mainloop.quit()
        else:
            self.working = False
            self.mainloop.quit()